//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SMMDriftCalculator.h"
using namespace Cephei::QL::Models::Marketmodels::Driftcomputation;
#include <gen/QL/Models/Marketmodels/Curvestates/CoterminalSwapCurveState.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels::Curvestates;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (Cephei::QL::Math::IMatrix^ pseudo, Cephei::IVector<Double>^ displacements, Cephei::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _Cpseudo;
    CoVector<Double>^ _Cdisplacements;
    CoVector<Double>^ _Ctaus;
    try
    {
#ifdef HANDLE
        _phSMMDriftCalculator = NULL;
#endif
        _Cpseudo = safe_cast<CMatrix^> (pseudo);
        _Cpseudo->Lock();
        QuantLib::Matrix& _pseudo = static_cast<QuantLib::Matrix&> (_Cpseudo->GetReference ()); 
        CoVector<Double>^ _Cdisplacements = safe_cast<CoVector<Double>^> (displacements);
        _Cdisplacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = _Cdisplacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        CoVector<Double>^ _Ctaus = safe_cast<CoVector<Double>^> (taus);
        _Ctaus->Lock();
        INativeVector<Double>^ _NCItaus = _Ctaus->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtaus = safe_cast<CDoubleVector^>(_NCItaus);
        std::vector<QuantLib::Time>& _taus = static_cast<std::vector<QuantLib::Time>&> (_NCtaus->GetReference ());
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire);
        QuantLib::Size _alive = (QuantLib::Size)ValueHelper::Convert (alive);
        _ppSMMDriftCalculator = new boost::shared_ptr<QuantLib::SMMDriftCalculator> (new QuantLib::SMMDriftCalculator ( _pseudo,  _displacements,  _taus,  _numeraire,  _alive ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpseudo != nullptr) _Cpseudo->Unlock();
        if (_Cdisplacements != nullptr) _Cdisplacements->Unlock();
        if (_Ctaus != nullptr) _Ctaus->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (boost::shared_ptr<QuantLib::SMMDriftCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSMMDriftCalculator = NULL;
#endif
	_ppSMMDriftCalculator = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (QuantLib::SMMDriftCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSMMDriftCalculator = NULL;
#endif
	_ppSMMDriftCalculator = new boost::shared_ptr<QuantLib::SMMDriftCalculator> (&childNative);
    
    _SMMDriftCalculatorOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (CSMMDriftCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSMMDriftCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSMMDriftCalculator = new boost::shared_ptr<QuantLib::SMMDriftCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSMMDriftCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CSMMDriftCalculator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (QuantLib::Handle<QuantLib::SMMDriftCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSMMDriftCalculator = &childNative;
	_ppSMMDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::SMMDriftCalculator>>(childNative.currentLink());
    
    _SMMDriftCalculatorOwner = owner;
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (QuantLib::Handle<QuantLib::SMMDriftCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSMMDriftCalculator = &childNative;
	_ppSMMDriftCalculator = &static_cast<boost::shared_ptr<QuantLib::SMMDriftCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::CSMMDriftCalculator (QuantLib::SMMDriftCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSMMDriftCalculator = NULL;
#endif
	_ppSMMDriftCalculator = new boost::shared_ptr<QuantLib::SMMDriftCalculator> (new QuantLib::SMMDriftCalculator (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::~CSMMDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSMMDriftCalculator != NULL)
    {
	    delete _ppSMMDriftCalculator;
        _ppSMMDriftCalculator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::!CSMMDriftCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSMMDriftCalculator != NULL)
    {
	    delete _ppSMMDriftCalculator;
    }
}
QuantLib::SMMDriftCalculator& Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::GetReference ()
{
    if (_ppSMMDriftCalculator == NULL) throw gcnew NativeNullException ();
	return **_ppSMMDriftCalculator;
}
boost::shared_ptr<QuantLib::SMMDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::GetShared ()
{
    if (_ppSMMDriftCalculator == NULL) throw gcnew NativeNullException ();
	return *_ppSMMDriftCalculator;
}
QuantLib::SMMDriftCalculator* Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::GetPointer ()
{
    if (_ppSMMDriftCalculator == NULL) throw gcnew NativeNullException ();
	return &**_ppSMMDriftCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SMMDriftCalculator>& Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::GetHandle ()
{
	if (_phSMMDriftCalculator == NULL)
	{
		_phSMMDriftCalculator = new Handle<QuantLib::SMMDriftCalculator> (*_ppSMMDriftCalculator);
	}
	return *_phSMMDriftCalculator;
}
#endif
bool Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::HasNative () 
{
	return (_ppSMMDriftCalculator != NULL);
}

Cephei::QL::Models::Marketmodels::Driftcomputation::ISMMDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator::Compute (Cephei::QL::Models::Marketmodels::Curvestates::ICoterminalSwapCurveState^ cs, Cephei::IVector<Double>^ drifts)
{
    CCoterminalSwapCurveState^ _Ccs;
    CoVector<Double>^ _Cdrifts;
    try
    {
        _Ccs = safe_cast<CCoterminalSwapCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CoterminalSwapCurveState& _cs = static_cast<QuantLib::CoterminalSwapCurveState&> (_Ccs->GetReference ()); 
        _Cdrifts = safe_cast<CoVector<Double>^> (drifts);
        _Cdrifts->Lock();
        INativeVector<Double>^ _NCIdrifts = _Cdrifts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdrifts = safe_cast<CDoubleVector^>(_NCIdrifts);
        std::vector<QuantLib::Real>& _drifts = static_cast<std::vector<QuantLib::Real>&> (_NCdrifts->GetReference ());
    	(*_ppSMMDriftCalculator)->compute ( _cs,  _drifts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (_Cdrifts != nullptr) _Cdrifts->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Driftcomputation::ISMMDriftCalculator^ Cephei::QL::Models::Marketmodels::Driftcomputation::CSMMDriftCalculator_Factory::Create (Cephei::QL::Math::IMatrix^ pseudo, Cephei::IVector<Double>^ displacements, Cephei::IVector<Double>^ taus, UInt64 numeraire, UInt64 alive)
{
    return gcnew CSMMDriftCalculator ( pseudo,  displacements,  taus,  numeraire,  alive);
}
